
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Deque;
import java.util.Scanner;

public class ConnectMySQL<where> {
	
    @SuppressWarnings("resource")
	public static void main(String[] args) {
        // JDBC鐨勪竴浜涢厤缃�
    	 
        String URL = "jdbc:mysql://localhost:3306/test";
        String driverName = "com.mysql.jdbc.Driver";
        String sql = "select * from student";
        String userName = "root";// root
        String password = "753951";// 123456
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        Scanner in=new Scanner(System.in); 
        int idl=0;
        
        //201521123030
       /* try {
        	
            Class.forName(driverName);// jdbc4.0 鍚庢棤闇�浣跨敤杩欏彞杩涜椹卞姩娉ㄥ唽鎿嶄綔
            conn = DriverManager.getConnection(URL, userName, password);
            pst = conn.prepareStatement("select * from students where id=?");
            pst.setInt(1, 4);
            rs = pst.executeQuery();*/
            
            // id | stuno | name | gender | birthdate | major
        long start = System.currentTimeMillis();
        try{
        	 Class.forName(driverName);
        	 conn=DriverManager.getConnection(URL, userName, password);
        	 conn.setAutoCommit(false);
        	String strsql=("insert into student(stuno,name) values(?,?)");
        	pst=conn.prepareStatement(strsql);
        	
        	 for (int i = 0; i < 1000; i++) {//1000数据
        		           
        		           pst.setString(1, "2015000");
        		           pst.setString(2, "杰克");
        		            
        		            // pst.setString(1,"f");
        		             pst.addBatch();
        		             if(i%1000==0){
        		             pst.executeBatch();}
           /* while (rs.next()) {
                int id = rs.getInt("id");
                String stuno = rs.getString("stuno");
                Date date = rs.getDate("birthdate");
                System.out.println("id=" + id + " stuno=" + stuno + " birthdate=" + date);
                
            }*/
        
            
           
        }} catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {/*涓嬮潰璧勬簮閲婃斁锛屽彲浠ヤ娇鐢╰ry..with..resources璇硶绠�鍖�*/
            if (rs != null) {
                try {
                    rs.close();
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pst != null) {
                try {
                    pst.close();
                    pst = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
            if (conn != null)
                try {
                    conn.close();
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                } 
            long end = System.currentTimeMillis();
            System.out.println("批量插入需要时间:"+(end - start));
        }
    }

	private static boolean rewriteBatchedStatements(String string, String string2, String string3) {
		// TODO Auto-generated method stub
		return false;
	}
}
